//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package wudashan.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import org.eclipse.californium.core.CoapClient;
import org.eclipse.californium.core.CoapResponse;
import org.eclipse.californium.core.CoapClient.Builder;
import org.eclipse.californium.core.coap.MediaTypeRegistry;
import org.eclipse.californium.core.network.Endpoint;
import wudashan.EDRXSimulator;

public class RegisterButton extends JButton {
    private static final String IP = "IP";
    private static final String EP = "EP";
    private static final String PSK = "PSK";
    private static final String FILENAME = "setting.properties";
    Properties properties = new Properties();

    public RegisterButton(final EDRXSimulator edrxSimulator) {
        super("注册设备");
        this.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String ip = edrxSimulator.getIpTextField().getText();
                String ep = edrxSimulator.getEpTextField().getText();
                String psk = edrxSimulator.getPskTextField().getText();
                boolean inputValid = ip != null && ip.length() != 0 && ep != null && ep.length() != 0 && psk != null && psk.length() != 0;
                if (!inputValid) {
                    JOptionPane.showMessageDialog(edrxSimulator, "请输入IP地址和设备EP码！");
                    return ;
                }

                edrxSimulator.getResource().setIsRegister(true);
                edrxSimulator.getServer().startServer();
                int port = edrxSimulator.isUseSecure() ? 5684 : 5683;
                CoapClient client = (new Builder(ip, port)).query("ep=" + ep).path(new String[]{"t", "r"}).create();
                client.setEndpoint(edrxSimulator.getServer().getEndpoints().get(0));
                CoapResponse response = client.post((byte[])null, MediaTypeRegistry.APPLICATION_OCTET_STREAM);
                if (response == null) {
                    edrxSimulator.getLogTextArea().printLog("CIG未响应，请检查CIG服务是否正常！");
                    return ;
                }

                edrxSimulator.getLogTextArea().printLog("CIG 响应码：%s.", response.getCode());
                if (!response.isSuccess()) {
                    JOptionPane.showMessageDialog(edrxSimulator, "注册设备失败，请查看CIG日志！");
                    return ;
                }

                edrxSimulator.setHasRegister(true);
                RegisterButton.super.setText("已注册");
                JOptionPane.showMessageDialog(edrxSimulator, "注册设备成功，可以开始接收命令或者上报数据！");
                RegisterButton.this.properties.setProperty("IP", edrxSimulator.getIpTextField().getText());
                RegisterButton.this.properties.setProperty("EP", edrxSimulator.getEpTextField().getText());
                RegisterButton.this.properties.setProperty("PSK", edrxSimulator.getPskTextField().getText());
                File file = new File("setting.properties");

                try {
                    FileOutputStream fo = new FileOutputStream(file);
                    Throwable var10 = null;

                    try {
                        RegisterButton.this.properties.store(fo, "store");
                    } catch (Throwable var20) {
                        var10 = var20;
                        throw var20;
                    } finally {
                        if (fo != null) {
                            if (var10 != null) {
                                try {
                                    fo.close();
                                } catch (Throwable var19) {
                                    var10.addSuppressed(var19);
                                }
                            } else {
                                fo.close();
                            }
                        }

                    }
                } catch (IOException var22) {
                    var22.printStackTrace();
                }
            }
        });
    }
}
